﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[NetworkSettings(channel=0, sendInterval=0f)]
public class IngameScene : NetworkBehaviour
{
    [CompilerGenerated]
    private static Func<bool> <>f__am$cache5;
    public const string IngameSceneName = "Ingame";
    [SyncVar]
    private bool isAllPlayersReady;
    private static Level level;
    private static string loadingLevelName;
    [SerializeField]
    private GameObject m_ui;
    [SyncVar]
    private string networkLoadingLevelName;

    private void Awake()
    {
    }

    public override float GetNetworkSendInterval()
    {
        return 0f;
    }

    [DebuggerHidden]
    private IEnumerator Init()
    {
        return new <Init>c__Iterator5 { <>f__this = this };
    }

    public static Coroutine Load(string levelName)
    {
        loadingLevelName = levelName;
        NetworkUtility.LoadScene("Ingame");
        if (<>f__am$cache5 == null)
        {
            <>f__am$cache5 = () => loadingLevelName == null;
        }
        return Singleton<IngameSceneHelper>.Instance.StartWaitUntil(<>f__am$cache5, null);
    }

    public override void OnDeserialize(NetworkReader reader, bool initialState)
    {
        if (initialState)
        {
            this.networkLoadingLevelName = reader.ReadString();
            this.isAllPlayersReady = reader.ReadBoolean();
        }
        else
        {
            int num = (int) reader.ReadPackedUInt32();
            if ((num & 1) != 0)
            {
                this.networkLoadingLevelName = reader.ReadString();
            }
            if ((num & 2) != 0)
            {
                this.isAllPlayersReady = reader.ReadBoolean();
            }
        }
    }

    private void OnDestroy()
    {
    }

    public override bool OnSerialize(NetworkWriter writer, bool forceAll)
    {
        if (forceAll)
        {
            writer.Write(this.networkLoadingLevelName);
            writer.Write(this.isAllPlayersReady);
            return true;
        }
        bool flag = false;
        if ((base.syncVarDirtyBits & 1) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.Write(this.networkLoadingLevelName);
        }
        if ((base.syncVarDirtyBits & 2) != 0)
        {
            if (!flag)
            {
                writer.WritePackedUInt32(base.syncVarDirtyBits);
                flag = true;
            }
            writer.Write(this.isAllPlayersReady);
        }
        if (!flag)
        {
            writer.WritePackedUInt32(base.syncVarDirtyBits);
        }
        return flag;
    }

    public static void Reload()
    {
        if (level != null)
        {
            Load(level.name);
        }
    }

    private void Start()
    {
        Singleton<IngameSceneHelper>.Instance.StartCoroutine(this.Init());
    }

    private void UNetVersion()
    {
    }

    public static bool isLoaded
    {
        get
        {
            return (!isLoading && (SceneManager.GetActiveScene().name == "Ingame"));
        }
    }

    public static bool isLoading
    {
        get
        {
            return !string.IsNullOrEmpty(loadingLevelName);
        }
    }

    public bool NetworkisAllPlayersReady
    {
        get
        {
            return this.isAllPlayersReady;
        }
        [param: In]
        set
        {
            base.SetSyncVar<bool>(value, ref this.isAllPlayersReady, 2);
        }
    }

    public string NetworknetworkLoadingLevelName
    {
        get
        {
            return this.networkLoadingLevelName;
        }
        [param: In]
        set
        {
            base.SetSyncVar<string>(value, ref this.networkLoadingLevelName, 1);
        }
    }

    [CompilerGenerated]
    private sealed class <Init>c__Iterator5 : IDisposable, IEnumerator, IEnumerator<object>
    {
        internal object $current;
        internal int $PC;
        private static Func<bool> <>f__am$cache6;
        private static Action<Player> <>f__am$cache7;
        private static Func<Instance, bool> <>f__am$cache8;
        private static Action<float> <>f__am$cache9;
        private static Func<Respawn, Vector3> <>f__am$cacheA;
        private static Func<bool> <>f__am$cacheB;
        private static Func<Player, bool> <>f__am$cacheC;
        private static Func<Player, bool> <>f__am$cacheD;
        private static Action<Text, int> <>f__am$cacheE;
        private static Func<Player, bool> <>f__am$cacheF;
        internal IngameScene <>f__this;
        internal Vector3 <respawn>__0;
        internal Text <statusContentPrefab>__2;
        internal Transform <statusGroup>__1;

        private static bool <>m__7A()
        {
            if (<>f__am$cacheC == null)
            {
                <>f__am$cacheC = x => x.sceneName != "Ingame";
            }
            return (Player.all.Count<Player>(<>f__am$cacheC) == 0);
        }

        private static void <>m__7B(Player a)
        {
            a.SetIsReady(false);
        }

        private static bool <>m__7C(Instance ins)
        {
            return !NetworkUtility.IsNetworkDynamic(ins);
        }

        private static void <>m__7D(float p)
        {
            UIProgressBar.Show("Loading...", p);
        }

        private static Vector3 <>m__7E(Respawn x)
        {
            return (x.transform.position + UnityEngine.Random.insideUnitSphere.SetY(0f));
        }

        internal bool <>m__7F()
        {
            HashSet<Player> all = Player.all;
            if (<>f__am$cacheD == null)
            {
                <>f__am$cacheD = x => !x.isReady;
            }
            bool flag = all.Count<Player>(<>f__am$cacheD) == 0;
            this.<statusGroup>__1.SetActive(!flag);
            if (<>f__am$cacheE == null)
            {
                <>f__am$cacheE = delegate (Text c, int i) {
                    Player player = Player.all.ElementAt<Player>(i);
                    c.text = string.Format("{0}: {1}", player.name, !player.isReady ? "<color=#FFFF00>Loading...</color>" : "Done.");
                    c.FindChild("LoadingMark").SetActive(!player.isReady);
                };
            }
            UIUtility.RefreshElements<Text>(this.<statusContentPrefab>__2, all.Count, this.<statusGroup>__1, <>f__am$cacheE);
            return flag;
        }

        private static bool <>m__80()
        {
            if (<>f__am$cacheF == null)
            {
                <>f__am$cacheF = x => !x.isReady;
            }
            return (Player.all.Count<Player>(<>f__am$cacheF) == 0);
        }

        internal bool <>m__81()
        {
            return this.<>f__this.isAllPlayersReady;
        }

        private static bool <>m__82(Player x)
        {
            return (x.sceneName != "Ingame");
        }

        private static bool <>m__83(Player x)
        {
            return !x.isReady;
        }

        private static void <>m__84(Text c, int i)
        {
            Player player = Player.all.ElementAt<Player>(i);
            c.text = string.Format("{0}: {1}", player.name, !player.isReady ? "<color=#FFFF00>Loading...</color>" : "Done.");
            c.FindChild("LoadingMark").SetActive(!player.isReady);
        }

        private static bool <>m__85(Player x)
        {
            return !x.isReady;
        }

        [DebuggerHidden]
        public void Dispose()
        {
            this.$PC = -1;
        }

        public bool MoveNext()
        {
            uint num = (uint) this.$PC;
            this.$PC = -1;
            switch (num)
            {
                case 0:
                    ScreenFader.CoverUp();
                    Time.timeScale = 0f;
                    if (Player.local != null)
                    {
                        break;
                    }
                    this.$current = Player.Create();
                    this.$PC = 1;
                    goto Label_0344;

                case 1:
                    break;

                case 2:
                    if (NetworkUtility.isServer)
                    {
                        if (<>f__am$cache7 == null)
                        {
                            <>f__am$cache7 = new Action<Player>(IngameScene.<Init>c__Iterator5.<>m__7B);
                        }
                        Player.all.Foreach<Player>(<>f__am$cache7);
                    }
                    if (NetworkUtility.isServer)
                    {
                        this.<>f__this.NetworknetworkLoadingLevelName = IngameScene.loadingLevelName;
                        goto Label_013A;
                    }
                    IngameScene.loadingLevelName = this.<>f__this.networkLoadingLevelName;
                    goto Label_012B;

                case 3:
                    goto Label_012B;

                case 4:
                    UIProgressBar.Clear();
                    goto Label_01DF;

                case 5:
                    goto Label_0262;

                case 6:
                    this.<>f__this.NetworkisAllPlayersReady = true;
                    goto Label_0302;

                case 7:
                    Time.timeScale = 1f;
                    ScreenFader.Fadeout();
                    IngameScene.loadingLevelName = null;
                    this.$PC = -1;
                    goto Label_0342;

                default:
                    goto Label_0342;
            }
            if (<>f__am$cache6 == null)
            {
                <>f__am$cache6 = new Func<bool>(IngameScene.<Init>c__Iterator5.<>m__7A);
            }
            this.$current = new WaitUntil(<>f__am$cache6);
            this.$PC = 2;
            goto Label_0344;
        Label_012B:
            while (string.IsNullOrEmpty(IngameScene.loadingLevelName))
            {
                IngameScene.loadingLevelName = this.<>f__this.networkLoadingLevelName;
                this.$current = null;
                this.$PC = 3;
                goto Label_0344;
            }
        Label_013A:
            IngameScene.level = Level.GetLevel(IngameScene.loadingLevelName);
            if (IngameScene.level != null)
            {
                if (!NetworkUtility.isServer)
                {
                    if (<>f__am$cache8 == null)
                    {
                        <>f__am$cache8 = new Func<Instance, bool>(IngameScene.<Init>c__Iterator5.<>m__7C);
                    }
                    IngameScene.level.loadFilter = <>f__am$cache8;
                }
                if (<>f__am$cache9 == null)
                {
                    <>f__am$cache9 = new Action<float>(IngameScene.<Init>c__Iterator5.<>m__7D);
                }
                this.$current = IngameScene.level.Load(<>f__am$cache9);
                this.$PC = 4;
                goto Label_0344;
            }
            object[] args = new object[] { IngameScene.loadingLevelName };
            UnityEngine.Debug.LogErrorFormat("Could not load level '{0}', it does not exist.", args);
        Label_01DF:
            if (<>f__am$cacheA == null)
            {
                <>f__am$cacheA = new Func<Respawn, Vector3>(IngameScene.<Init>c__Iterator5.<>m__7E);
            }
            this.<respawn>__0 = Respawn.all.Select<Respawn, Vector3>(<>f__am$cacheA).GetRandomElement<Vector3>();
            if (Player.local.character == null)
            {
                this.$current = Player.local.CreateCharacter(this.<respawn>__0);
                this.$PC = 5;
                goto Label_0344;
            }
            Player.local.character.SetPosition(this.<respawn>__0);
        Label_0262:
            this.<statusGroup>__1 = this.<>f__this.m_ui.FindChild("LoadingStatus");
            this.<statusContentPrefab>__2 = this.<statusGroup>__1.FindChild<Text>("Content");
            this.<>f__this.StartWaitUntil(new Func<bool>(this.<>m__7F), null);
            Player.local.SetIsReady(true);
            if (NetworkUtility.isServer)
            {
                if (<>f__am$cacheB == null)
                {
                    <>f__am$cacheB = new Func<bool>(IngameScene.<Init>c__Iterator5.<>m__80);
                }
                this.$current = new WaitUntil(<>f__am$cacheB);
                this.$PC = 6;
                goto Label_0344;
            }
        Label_0302:
            this.$current = new WaitUntil(new Func<bool>(this.<>m__81));
            this.$PC = 7;
            goto Label_0344;
        Label_0342:
            return false;
        Label_0344:
            return true;
        }

        [DebuggerHidden]
        public void Reset()
        {
            throw new NotSupportedException();
        }

        object IEnumerator<object>.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }

        object IEnumerator.Current
        {
            [DebuggerHidden]
            get
            {
                return this.$current;
            }
        }
    }
}

